﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IntelligentEscalator.BUS;
using IntelligentEscalator.Models;

namespace IntelligentEscalator.DAL
{
    public class HomeDAL
    {
        public int QueryInspectAnnually()
        {
            string strsql = string.Format("SELECT COUNT(1) FROM vi_maintaininfo WHERE maintenancestateId!=4 AND maintenanceDatetime<'{0}' AND maintenanceProperties='年度保养' AND maintenanceDatetime>'{1}'", DateTime.Now.AddDays(60).ToString("yyyy-MM-dd HH:mm:ss"), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            return SqlHelper<MaintainInfo>.SqlScalar(strsql);
        }

        public int QueryFault()
        {
            //string strsql = "SELECT COUNT(1) FROM repair_workorder WHERE workorderStateID=0";
            string strsql = "SELECT COUNT(1) FROM vi_fault WHERE isRead=0";
            return SqlHelper<RepairWorkOrderInfo>.SqlScalar(strsql);
        }

        public int QueryEarlyWarning()
        {
            string strsql = "SELECT COUNT(1) FROM elevator WHERE bend>bendThreshold*0.9 AND bendThreshold>0";
            return SqlHelper<Elevator>.SqlScalar(strsql);
        }

        public List<MaintainInfo> QueryInspectAnnuallyList()
        {
            string strsql =string.Format("SELECT *,DATEDIFF(maintenanceDatetime, '{1}') AS timedifference FROM vi_maintaininfo WHERE maintenancestateId=0 AND maintenanceDatetime<'{0}' AND maintenanceProperties='年度保养' AND maintenanceDatetime>'{1}' AND maintenancestateId!=4", DateTime.Now.AddDays(60).ToString("yyyy-MM-dd HH:mm:ss"),DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            return SqlHelper<MaintainInfo>.SqlList(strsql);
        }

        public List<RepairWorkOrderInfo> QueryFaultList()
        {
            string strsql = "SELECT elevatorName,faultCode AS faultCause,faultTime as reportTime,id FROM vi_fault WHERE isRead=0";
            return SqlHelper<RepairWorkOrderInfo>.SqlList(strsql);
        }

        public List<Elevator> QueryEarlyWarningList()
        {
            string strsql = "SELECT * FROM elevator WHERE bend>bendThreshold*0.9 AND bendThreshold>0";
            return SqlHelper<Elevator>.SqlList(strsql);
        }
    }
}
